/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "Stdafx.h"
#include <string>
#include "TypeDef.h"
#include "HelperFunctions.h"
#include "SearchEngineComHdr.h"
#include "boost\regex.hpp"
#include "SogouPageParser.h"
using namespace std;
using namespace boost;

void CSogouSongParser::Parse(const tstring & content)
{
     // Clear the results set
     m_SongList.clear();

     // Regex repression        
     tregex reg_song(_T("<td class=\"songname\"><a.+?entitle=.+?title=\"(.*?)\".+?")//name
                     _T("<td class=\"singger\">(.+?)</td>.*?")//singer
                     _T("<td class=\"singger\"><a.+?entitle=.+?title=\"(.*?)\".+?")//album
                     _T("window.open\\(\'(.+?)\'.+?")//url
                     _T("<td.+?/td>.*?")//lrc           
                     _T("<td.+?/td>.*?")//cellphone ring
                     _T("<td>(.*?)<.+?")//size
                     _T("<td>(.*?)<")); //format

     // May be there are more than one singer
     tregex reg_singer (_T("entitle=.+?title=\"(.*?)\"")); 

     tsmatch song;
     tsmatch singer;

     tstring::const_iterator bgn = content.begin();
     tstring::const_iterator end = content.end();

     int Index = 0;
     while( regex_search(bgn, end, song, reg_song) )
     {
            ++Index;
            tstring name = song[4].str();

            bgn = song[0].second;
            // There may be more than one singer, so loop to get them
            tstring str = song[2];            
            tstring::const_iterator tmp_bgn = str.begin();
            tstring::const_iterator tmp_end = str.end();
            tstring singers;
            while( regex_search(tmp_bgn, tmp_end, singer, reg_singer) )
            {
                   tmp_bgn = singer[0].second;
                   singers += singer[1].str() + _T(" ");
            }

            SongProperty Sp = SongProperty(IntToStr(Index),//index
                                         song[1].str(),    //name
                                         song[4].str(),    //song url
                                         singers,          //singer name
                                         song[3].str(),    //album 
                                         song[5].str(),    //size in string fromat
                                         song[6].str());   //format
                                 
            m_SongList.push_back(Sp);
     }
}

void CSogouSongParser::GetSongList(SongList & List)const
{
     List.clear();
     unsigned int Size = m_SongList.size();
     if( Size == 0 ) return;

     List.reserve(Size);
     for( unsigned int i = 0; i < Size; ++i )
     {        
          SongProperty Sp = m_SongList[i];
          List.push_back(Sp);
     }
}


void CSogouSongNumParser::Parse(const tstring & Content)
{
     // Regex repression
     tregex Reg_SongNum(_T("resultcnt.*?\"(\\d+)\""));
     tsmatch Num;
     if( regex_search(Content.begin(), Content.end(), Num, Reg_SongNum) )
         m_TotalSongNum = _ttoi(Num[1].str().c_str());
     else
         m_TotalSongNum = 0;
}

void CSogouSongUrlParser::Parse(const tstring & Content)
{
     tregex Reg_Url(_T("<td><a title=.+?href=\"(.+?)\">"));
     tsmatch Url;
     if( regex_search(Content,  Url, Reg_Url) )
         m_DownloadUrl = Url[1].str();
}
